package user

import (
	"huzhen/controllers/db"
	ctrl "huzhen/controllers"
	"net/http"
	"fmt"
	"bytes"
	"encoding/base64"
	"image"
	"image/jpeg"
	"strconv"
	"os"
	"encoding/json"
)

//	"shopman/models"

func InitHandlers() {
	ctrl.HMAP["/app/user/create"] = create
	ctrl.HMAP["/app/user/login"] = login
	ctrl.HMAP["/app/user/test"] = test
}
func login(w http.ResponseWriter, r *http.Request) {
	/*u, err := url.Parse(r.URL)*/
	r.ParseForm()
	w.Header().Set("Access-Control-Allow-Origin", "*")
	accounts := r.FormValue("accounts")
	password := r.FormValue("password")
	sql := "SELECT * FROM users where accounts='"+accounts+"'"+"AND password='"+password+"'"
	rowArray := db.Query(sql)
	if(len(rowArray)>0){
		coms := make([]map[string]string,1)
		for i, row := range rowArray {
			coms[i]=make(map[string]string)
			for k,v:=range row{
				coms[i][k]=string(v)
			}
		}
		js1,_ := json.Marshal(coms)
		fmt.Println(string(js1))
		w.Write([]byte("{'res':0,'data':"+string(js1)+"}"))
	}else{
		w.Write([]byte("{'res':1,'msg':'not login!!'}"))
	}
}
func test(w http.ResponseWriter, r *http.Request) {
}

func create(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	w.Header().Set("Access-Control-Allow-Origin", "*")
	accounts := r.FormValue("accounts")
	password := r.FormValue("password")
	name := r.FormValue("name")
	phone := r.FormValue("phone")
	building := "第"+r.FormValue("building")+"栋"
	dorm := r.FormValue("dorm")
	have_img := r.FormValue("have_img")
	user_img := r.FormValue("user_img")
	user_top,_:= strconv.ParseFloat(r.FormValue("user_top"),32)
	user_left,_:= strconv.ParseFloat(r.FormValue("user_left"),32)
	user_width,_:= strconv.ParseFloat(r.FormValue("user_width"),32)
	user_height,_:= strconv.ParseFloat(r.FormValue("user_height"),32)
	old_height,_:= strconv.ParseFloat(r.FormValue("old_height"),32)
	old_width,_:= strconv.ParseFloat(r.FormValue("old_width"),32)
	sql := "SELECT accounts FROM users where accounts='"+accounts+"'"
	rowArray1 := db.Query(sql)
	if(len(rowArray1)>0){
		fmt.Println("this user is be created")
		w.Write([]byte("{'res':1,'msg':'该用户已存在'}"))
	}else{
		sql = "INSERT INTO users (accounts,password,name,phone,have_img,building,dorm) values('"+accounts+"','"+password+"','"+name+"','"+phone+"','"+have_img+"','"+building+"','"+dorm+"')"//新用户10元体验
		fmt.Println(sql)
		_ = db.Query(sql)
		if(have_img=="1") {
			ddd, _ := base64.StdEncoding.DecodeString(user_img)
			bbb := bytes.NewBuffer(ddd)
			m, _, _ := image.Decode(bbb)
			rgbImg := m.(*image.YCbCr)
			if (old_width > 420) {
				user_left = user_left * (old_width / 420)
				user_width = user_width * (old_width / 420)
			}
			if (old_height>420) {
				user_top = user_top * (old_height / 420)
				user_height = user_height * (old_height / 420)
			}
			if(user_width>user_height){
				user_height=user_height
			}else {
				user_width=user_height
			}
			subImg := rgbImg.SubImage(image.Rect(int(user_left), int(user_top), int(user_width + user_left), int(user_height + user_top))).(*image.YCbCr)//(x0,y0,x1,y1)
			f, _ := os.Create("./img/usrimg/" + accounts + ".jpg")
			jpeg.Encode(f, subImg, nil)
			defer f.Close()
		}
		w.Write([]byte("{'res':0,'msg':'注册用户成功'}"))
		fmt.Println("create OK!!")
	}
}

